package org.demo;

import com.rabbitmq.client.*;
import org.util.RabbitMQUtil;

import java.io.IOException;

/**
 * @author yexi
 */
public class Consumer {

    public static void main(String[] args) throws Exception {
        //1. 获取连接对象
        Connection connection = RabbitMQUtil.getConnection();

        //2. 构建Channel
        Channel channel = connection.createChannel();

        //3. 构建队列
        channel.queueDeclare("testQueue", false, false, false, null);

        //4. 监听消息
        DefaultConsumer callback = new DefaultConsumer(channel) {
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                System.out.println("消费者获取到消息：" + new String(body, "UTF-8"));
            }
        };
        channel.basicConsume("testQueue", true, callback);
        System.out.println("开始监听队列");

    }
}